package client.comm.baoding.ui

import android.content.IntentFilter
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.adapter.CateNewAdapter
import client.comm.baoding.adapter.NewSyAdapter
import client.comm.baoding.adapter.SyPagingHeader2Adapter
import client.comm.baoding.api.bean.CateGoods
import client.comm.baoding.api.bean.GoodsIndex
import client.comm.baoding.databinding.ActivityTabone2NewBinding
import client.comm.baoding.receiver.MeBroadcast
import client.comm.baoding.ui.vm.MainViewModel
import client.comm.commlib.comm_ui.draw.SpaceItemBottomDecoration
import client.comm.commlib.toast.toast
import client.comm.commlib.widget.EmptyLayout
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlin.properties.Delegates

class TabOne2Activity : BaseMiddleActivity<MainViewModel, ActivityTabone2NewBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_tabone2_new
    }

    val adapter2: NewSyAdapter by lazy {
        NewSyAdapter(this, mImgLoader).apply {

            refreshLoading = {
                if (itemCount == 0) {
//                    binding.emptyLayout2.errorType = EmptyLayout.NETWORK_LOADING
                }
            }
            refreshNotLoading = {
                binding.refreshLayout2.isRefreshing = false
                if (itemCount == 0) {
//                    binding.emptyLayout2.errorType = EmptyLayout.NODATA
                } else {
//                    binding.emptyLayout2.visibility = View.GONE
                }
            }
            refreshError = {
                if (itemCount == 0)
//                    binding.emptyLayout2.errorType = EmptyLayout.NETWORK_ERROR
                    toast((it).error.message)
            }

        }
    }

    val headerAdapter2 by lazy {
        SyPagingHeader2Adapter(mImgLoader, position, this).apply {

        }
    }

    val goodAdapter: CateNewAdapter by lazy {
        CateNewAdapter(this, mImgLoader).apply {

        }
    }

    lateinit var cate_id: String
    var position by Delegates.notNull<Int>()

    override fun initViewModel(): MainViewModel = createViewModel()

    val meBroadcast: MeBroadcast by lazy {
        MeBroadcast()
    }

    override fun initView() {
        registerReceiver(meBroadcast, IntentFilter(MeBroadcast.MAIN_SWITCH_TAB))
        intent.getStringExtra("title_name")?.let {
            binding.titleName = it
        }
        intent.getStringExtra("cate_id")?.let {
            cate_id = it
        }
        intent.getIntExtra("position", -1).let {
            position = it
        }


        binding.event = this
        binding.recyclerView2.adapter = adapter2.withLoadStateHeader(
//            footer = PagerFooterAdapter(),
            header = headerAdapter2
        )
//        binding.emptyLayout2.setOnClickListener {
//            adapter2.refresh()
//        }
        binding.refreshLayout2.setOnRefreshListener {
            adapter2.refresh()
            viewModel.pageCateNew.value = 0
            viewModel.holdCateNew(cate_id)


        }

        lifecycleScope.launch {
            viewModel.pagingDataUser2().collect {
                adapter2.submitData(it)
            }
        }
        val gridLayoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)

        binding.recyclerView2.layoutManager = gridLayoutManager

        viewModel.home2Call.observe(this, Observer {
            it?.let {
                if (it.isSuccess) {
                    headerAdapter2.goodsIndex = it.data!!

                    val list = it.data!!.tj_goods_list as MutableList<GoodsIndex.TjGoods?>
                    if (list.size > 0) {
//                        adapter2.submitData(list, it.data!!)
//                        binding.emptyLayout2.visibility = View.GONE
                    }
                } else {
                    if (adapter2.itemCount == 0) {
//                        binding.emptyLayout2.errorType = EmptyLayout.NETWORK_ERROR
                    }
                }
            }
        })

        viewModel.callCateNew.observe(this, Observer {
            it?.apply {
                if (it.isSuccess) {
                    val list =
                        it.data?.goods_list as MutableList<CateGoods.Goods?>?
                    if (viewModel.pageCateNew.value == 1) {
                        goodAdapter.submitList(list)
                    } else
                        goodAdapter.appendList(list)
                    if (list != null && list.size > 0) {
                        if (list.size < 10) {
                            goodAdapter.hasMoreState.value = false
                        }
                        binding.emptyLayoutList.visibility = View.GONE
                    } else {
                        if (goodAdapter.itemCount == 0)
                            binding.emptyLayoutList.errorType = EmptyLayout.NODATA
                        goodAdapter.hasMoreState.value = true
                    }
                } else {
                    if (goodAdapter.itemCount == 0)
                        binding.emptyLayoutList.errorType = EmptyLayout.NETWORK_ERROR
                }

            }
        })

        //list
        binding.recyclerViewList.apply {
            adapter = goodAdapter
            val gridLayoutManager =
                StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
            layoutManager = gridLayoutManager
            addItemDecoration(
                SpaceItemBottomDecoration(
                    10
                )
            )
//            onLoadMore = {
//                this@CateNewChildFragment.adapter.retry()
//            }
        }

//        binding.emptyLayoutList.setOnClickListener {
//            viewModel.holdCateNew(cate_id)
//
//        }
        viewModel.holdCateNew(cate_id)

    }

}

